﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ZyGames.DZPocker.Tools
{
	public static class Tool
	{
		#region 获得不同的字符串
		//数字+字母
		private const string CHAR1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		//纯数字
		private const string CHAR2 = "0123456789";

		public static List<String> GenerateDiffNum(int len, int count)
		{
			double max_value = Math.Pow(36, len);
			if (max_value > long.MaxValue)
			{
				return null;
			}

			long all_count = (long)max_value;
			long stepLong = all_count / count;
			if (stepLong > int.MaxValue)
			{
				return null;
			}
			int step = (int)stepLong;
			if (step < 3)
			{
				return null;
			}
			long begin = 0;
			List<string> list = new List<string>();
			Random rand = new Random();
			while (true)
			{
				long value = rand.Next(1, step) + begin;
				begin += step;
				list.Add(GetChart(len, value, true));
				if (list.Count == count)
				{
					break;
				}
			}
			list = SortByRandom(list);
			return list;
		}

		/// <summary>
		/// 随机排序
		/// </summary>
		/// <param name="charList"></param>
		/// <returns></returns>
		private static List<string> SortByRandom(List<string> charList)
		{
			Random rand = new Random();
			for (int i = 0; i < charList.Count; i++)
			{
				int index = rand.Next(0, charList.Count);
				string temp = charList[i];
				charList[i] = charList[index];
				charList[index] = temp;
			}
			return charList;
		}

		/// <summary>
		/// 将数字转化成字符串
		/// </summary>
		/// <param name="len"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		private static string GetChart(int len, long value, bool isNum = false)
		{
			StringBuilder str = new StringBuilder();
			string CHARStr = isNum ? CHAR2 : CHAR1;
			while (true)
			{
				str.Append(CHARStr[(int)(value % CHARStr.Length)]);
				value = value / CHARStr.Length;
				if (str.Length == len)
				{
					break;
				}
			}
			return str.ToString();
		} 
		#endregion

		#region 正则表达式
		/// <summary>
		/// 验证输入为整数
		/// </summary>
		/// <param name="strNum">输入字符</param>
		/// <returns>返回一个bool类型的值</returns>
		public static bool ValidateNum(string strNum)
		{
			return Regex.IsMatch(strNum, "^[0-9]*$");
		}
		#endregion

		#region 控制台日志
		public static void WriteErrorLine(string str)
		{
			Console.ForegroundColor = ConsoleColor.Red;
			Console.WriteLine(str);
			Console.ForegroundColor = ConsoleColor.White;
		}

		public static void WriteWarmingLine(string str)
		{
			Console.ForegroundColor = ConsoleColor.Yellow;
			Console.WriteLine(str);
			Console.ForegroundColor = ConsoleColor.White;
		}

		/// <summary>
		/// 打印普通控制台日志
		/// </summary>
		/// <param name="str"></param>
		public static void WriteLine(string str)
		{
			Console.WriteLine(str);
		}
		#endregion
	}
}
